package com.mo.gateway.filter;

import com.mo.global.utils.LoginToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter;
import org.springframework.core.Ordered;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther:Lang
 * @Date:2022-12-09
 * @Time:14:34
 * @Description:TODO
 */
@Component
public class loginFilter implements GlobalFilter, Ordered {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //从请求头里获取uri
        String uri = exchange.getRequest().getURI().toString();

        //若uri 包含 登录，退出，检查是否登录则直接放行
        if (uri.contains("/system/permission/login")
                || uri.contains("/system/permission/loginOut")
                || uri.contains("/system/permission/checkStaff")) {
            return chain.filter(exchange);
        }

        HttpHeaders headers = exchange.getRequest().getHeaders();
        List<String> idList = headers.get("id");
        List<String> tokenList = headers.get("token");
        String id;
        String strToken;

        //判断登录状态
        //1、未登录
        if (idList == null || tokenList == null) {
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete();
        } else {
            id = idList.get(0);
            strToken = tokenList.get(0);
            if (id == null || strToken == null) {
                exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
                return exchange.getResponse().setComplete();
            }
            ;
        }
        //获取redis 缓存里的loginToken
        Map<String, LoginToken> loginToken = (Map<String, LoginToken>) redisTemplate.opsForValue().get("loginToken");
        if (loginToken == null) loginToken = new HashMap<>();

        LoginToken token = loginToken.get(id);
        if (token == null || !strToken.equals(token.getToken())) {
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete();
        }
        String roleId = String.valueOf(token.getRole());
        ServerHttpRequest request = exchange.getRequest().mutate().header("roleId", roleId).build();

        return chain.filter(exchange.mutate().request(request).build());

    }

    @Override
    public int getOrder() {
        return NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER - 10;
    }
}
